
export const useImmer = () => {
    // 简单实现 immer 核心代理
    const reduce = <T extends object>(rawData: T, cb: (data: T) => void) => {

        const modified: Record<string, any> = {}

        const handler: ProxyHandler<T> = {
            get(target: any, key: string) {
                if (key in modified) {
                    return modified[key]
                }

                if (typeof target[key] === 'object' && target[key] !== null) {
                    return new Proxy(target[key], handler)
                }
                return target[key]
            },

            set(_, key: string, newValue) {
                modified[key] = newValue
                return true
            }
        }

        const proxy = new Proxy(rawData, handler)

        cb(proxy)

        if (Object.keys(modified).length === 0) {
            return rawData
        }

        return modified
    }

    return {
        reduce
    }
}